var tableElem = "LAY-home-cms-list";
var formElem = "LAY-home-cms-listform";
var tableToolsElem = "LAY-home-cms-tools";
var formSubmitElem = "LAY-home-cms-listform-submit";
var baseUrl = window.globalConfig.url_cms_cat_list

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //分类管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'cat_id', width: 100, title: '分类Id', fixed: 'left', sort: true}
            , {
                field: 'navigateName', title: "导航名称", templet: function (d) {
                    return d.navigate ? d.navigate.name : "无"
                }
            }
            , {field: 'cat_name', title: '分类名称', minWidth: 100, edit: 'text'}
            , {field: 'urlrewriter', title: '静态化url', edit: 'text'}
            , {
                field: 'is_show', title: '显示', templet: "#ID-table-cmsCat-is_show-switch"
            }
            , {field: 'size_suggest', title: '封面尺寸建议', edit: 'text'}
            , {field: 'sort_order', title: '排序', edit: 'text'}
            , {
                field: 'cmsList', title: "详情数量", templet: function (d) {
                    return d.cmsList.length
                }
            }
            , {title: '操作', minWidth: 280, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'}
    });

    var active = {
        //添加
        add: function (othis) {
            admin.popup({
                title: '添加新闻'
                , area: ['550px', '750px']
                , id: 'LAY-popup-content-add'
                , success: function (layero, index) {
                    view(this.id).render('home/article/listform').done(function () {
                        form.render(null, formElem);

                        $("#rewrite_url").val(common.generateRandomRewriteUrl())

                        //提交
                        form.on(`submit(${formSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: baseUrl,
                                type: "POST",
                                data: field,
                                success: function (res) {
                                    common.ctmMsg(res.code, res.msg)
                                    tableItem.reloadData();
                                    layer.close(index); //执行关闭
                                }
                            })
                        });
                    });
                }
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除分类
            layer.confirm('确定删除此分类？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.cat_id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData();
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改分类
            admin.popup({
                title: '编辑分类'
                , area: ['550px', '750px']
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {


                    admin.req({
                        url: window.globalConfig.url_navigate_list + "/" + data.nav_id,
                        type: "GET",
                        success: (res) => {
                            $nav = res.data
                            data.navName = $nav.name
                            data.nav_id = $nav.id
                            view(this.id).render('home/article/listform', data).done(function () {
                                form.render(null, formElem);

                                setTimeout(() => {
                                    form.val(formElem, {
                                        nav_id: data.nav_id,
                                    })
                                }, 800)

                                //提交
                                form.on(`submit(${formSubmitElem})`, function (data) {
                                    var field = data.field; //获取提交的字段

                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    // 修改分类的请求
                                    admin.req({
                                        url: baseUrl + "/" + field.cat_id,
                                        type: "PUT",
                                        data: field,
                                        success: function (res) {
                                            layer.msg(res.msg)
                                            tableItem.reloadData();
                                            layer.close(index); //执行关闭
                                        }
                                    })
                                });
                            });
                        }
                    });
                }
            });
        } else if (obj.event === 'copy') { // 复制新闻类型
            layer.confirm('确定复制此新闻类型？', function (index) {
                var field = data
                field.cat_name = field.cat_name + "_copy"
                field.urlrewriter = common.generateRandomRewriteUrl()
                delete field.cat_id
                delete field.show_in_nav
                delete field.is_review
                delete field.is_mobile
                admin.req({
                    url: baseUrl,
                    type: "POST",
                    data: field,
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        if (res.code === 0) {
                            tableItem.reloadData()
                        }
                    }
                })
            });
        }
    });


    // 表格文本修改
    common.tableTextEditor(tableElem, baseUrl, 'cat_id')
    common.tableSwitchEditor('cmsCat-is_show-status', baseUrl)

});